package com.ymatou.iapi.marketingmanage.testcase;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.marketingmanage.parameter.GetPromotionByIdBean;
import com.ymatou.iapi.marketingmanage.parameter.PromotionPolicy;
import com.ymatou.iapi.marketingmanage.parameter.PromotionValue;
import com.ymatou.iapi.marketingmanage.service.GetProductPromotionPolicyCall;
import com.ymatou.iapi.marketingmanage.service.GetPromotionByIdCall;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.database.mongo.YmtBuyerProductWapper;
import com.ymttest.utils.DiffUtil;

/**
 * 促销活动详情 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetPromotionById {
	private static GetPromotionByIdBean getpromotionbyidBean;
	private static GetPromotionByIdCall getpromotionbyidCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("促销活动详情");
	}

	@Before
	public void caseUp() {
		getpromotionbyidBean = new GetPromotionByIdBean();
		getpromotionbyidCall = new GetPromotionByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpromotionbyidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyData(GetPromotionByIdCall getpromotionbyidCall2, CreateSellerPromotionBean cspb) {
		JsonObject json = getpromotionbyidCall2.getJsonObject().getAsJsonObject("Data");
		JsonObject recjson = json.get("PromotionInfo").getAsJsonObject();
		PromotionPolicy recp = (PromotionPolicy) FormatUtil.GSONToObject(recjson.toString(), PromotionPolicy.class);
		PromotionPolicy sendp = cspb.getPromotionInfo();
		Logger.verifyEquals(recp.getPromotionName(), sendp.getPromotionName(), "验证PromotionName");
		Logger.verifyEquals(recp.getPromotionDesc(), sendp.getPromotionDesc(), "验证PromotionDesc");
		Logger.verifyEquals(recp.getPromotionBeginTime().substring(0, 12), sendp.getPromotionBeginTime().substring(0, 12), "验证PromotionBeginTime");
		Logger.verifyEquals(recp.getPromotionEndTime().substring(0, 12), sendp.getPromotionEndTime().substring(0, 12), "验证PromotionEndTime");
		Logger.verifyEquals(recp.getPromotionType(), sendp.getPromotionType(), "验证PromotionType");
		Logger.verifyEquals(recp.getIsSameProductUse(), sendp.getIsSameProductUse(), "验证IsSameProductUse");
		PromotionValue spv = sendp.getPromotionValues().get(0);
		PromotionValue rpv = recp.getPromotionValues().get(0);
		Integer type = sendp.getPromotionType();
		if (type == 1) {
			Logger.verifyEquals(rpv.getQtyCondition(), spv.getQtyCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue(), "验证dv");

		}
		// 折扣的时候/10
		else if (type == 2) {
			Logger.verifyEquals(rpv.getQtyCondition(), spv.getQtyCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue().divide(BigDecimal.valueOf(10)), "验证dv");

		} else if (type == 3) {
			Logger.verifyEquals(rpv.getAmountCondition(), spv.getAmountCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue(), "验证dv");
		} else if (type == 4) {
			Logger.verifyEquals(rpv.getAmountCondition(), spv.getAmountCondition(), "验证rv");
			Logger.verifyEquals(rpv.getValue(), spv.getValue().divide(BigDecimal.valueOf(10)), "验证dv");

		} else if (type == 5) {
			Logger.verifyEquals(2, spv.getQtyCondition(), "验证rv");
			Logger.verifyEquals(new BigDecimal(0.5), spv.getValue(), "验证dv");

		}

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetPromotionById_001() {
		Logger.start(true, "按sellerid和PromotionId查询促销活动详情成功ReduceIfCountEnough");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyEquals("操作成功", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionbyidCall.getString("BCode"), "验证BCode");
			verifyData(getpromotionbyidCall, cspb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetPromotionById_002() {
		Logger.start(true, "不输入sellerid，失败");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			// getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyIsContains("无效的买手Id", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", getpromotionbyidCall.getString("BCode"), "验证BCode");
			// verifyData(getpromotionbyidCall, cspb);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetPromotionById_003() {
		Logger.start(true, "不输入PromotionId，失败");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			// getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyIsContains("无效的促销编号", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", getpromotionbyidCall.getString("BCode"), "验证BCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetPromotionById_004() {
		Logger.start(true, "PromotionId和sellerid不匹配,失败");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid + 1);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyEquals("操作成功", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionbyidCall.getString("BCode"), "验证BCode");
			JsonElement PromotionInfo = getpromotionbyidCall.getJsonObject().getAsJsonObject("Data").getAsJsonObject().get("PromotionInfo");
			Logger.verifyEquals("null", PromotionInfo.toString(), "验证PromotionInfo为空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetPromotionById_005() {
		Logger.start(true, "按sellerid和PromotionId查询促销活动详情成功ReduceIfCountEnough");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfCountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			cspb.getPromotionInfo().setCanUseSellerCoupon(true);
			cspb.getPromotionInfo().setIsLimitProduct(true);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyEquals("操作成功", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionbyidCall.getString("BCode"), "验证BCode");
			verifyData(getpromotionbyidCall, cspb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetPromotionById_006() {
		Logger.start(true, "按sellerid和PromotionId查询促销活动详情成功ReduceIfAmountEnough");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			cspb.getPromotionInfo().setCanUseSellerCoupon(true);
			cspb.getPromotionInfo().setIsSameProductUse(true);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyEquals("操作成功", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionbyidCall.getString("BCode"), "验证BCode");
			verifyData(getpromotionbyidCall, cspb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetPromotionById_007() {
		Logger.start(true, "按sellerid和PromotionId查询促销活动详情成功DiscountIfAmountEnough");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			cspb.getPromotionInfo().setCanUseSellerCoupon(true);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyEquals("操作成功", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionbyidCall.getString("BCode"), "验证BCode");
			verifyData(getpromotionbyidCall, cspb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetPromotionById_008() {
		Logger.start(true, "按sellerid和PromotionId查询促销活动详情成功HalfIfCountEnough");
		try {
			int sellerid = 102;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.HalfIfCountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			String pid = bpw.get(0).get("ProductId").toString();
			cspb.getPromotionInfo().getProductIds().add(pid);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			int proid = JSellerPromotionCallService.CreateSellerPromotion(cspb);
			getpromotionbyidBean.setPromotionId(proid);
			getpromotionbyidBean.setSellerId(sellerid);
			getpromotionbyidCall.setData(getpromotionbyidBean);
			getpromotionbyidCall.callService();
			Logger.verifyEquals("操作成功", getpromotionbyidCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", getpromotionbyidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", getpromotionbyidCall.getString("BCode"), "验证BCode");
			verifyData(getpromotionbyidCall, cspb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}